package own.stu.inter.crawler;

import java.util.ArrayList;
import java.util.List;

public class ContentAnalyze {

    static String host = "https://m.ahfgb.com";
    static String next_page_start_flag = "<a id=\"pt_next\" class=\"chapterPageButton\" href=\"";
    static String next_page_end_flag = "\">下一页</a>";

    public static void main(String[] args) {
        String body = "</span>\n" +
                "<span class=\"right\">\n" +
                "<a href=\"/25/25741/index_2.html\" class=\"onclick\">下一页</a>\n" +
                "</span>";


        System.out.println(getNextDirList(body));

//        System.out.println(getTitle(body));
       /* String content = getContent(body);
        System.out.println(content);
        String nextPageOfChapter = getNextPageOfChapter(body);
        System.out.println(nextPageOfChapter);*/

        /*String content = getContent(body, dir_start_flag, dir_end_flag);
        List<String> list = getDirList(content);
        System.out.println(list);*/
    }

    public static String getNextPageOfChapter(String body) {
        int startIdx = body.lastIndexOf(next_page_start_flag);
        if (startIdx < 0) {
            return "";
        }

        startIdx += next_page_start_flag.length();

        int endIdx = body.indexOf(next_page_end_flag, startIdx);
        if (endIdx < 0) {
            return "";
        }

        return host + body.substring(startIdx, endIdx);
    }

    static String content_start_flag = "<div id=\"nr1\">";
    static String content_end_flag = "</div>";

    public static String getContent(String body, String startFlag, String endFlag) {
        int startIdx = body.indexOf(startFlag);
        if (startIdx < 0) {
            return "";
        }

        startIdx += startFlag.length();
        int endIdx = body.indexOf(endFlag, startIdx);

        if (endIdx < 0) {
            return "";
        }

        return body.substring(startIdx, endIdx);
    }

    public static String getContent(String body) {
        String content = getContent(body, content_start_flag, content_end_flag);
        return content.replace("<br><br>", "\n").replace("m.ahfgb.com", "");
    }

    static String title_start_flag = "<div class=\"nr_title\" id=\"nr_title\">";
    static String title_end_flag = "</div>";

    public static String getTitle(String body) {
        return getContent(body, title_start_flag, title_end_flag);
    }

    static String dir_start_flag = "<div class=\"intro\">正文</div>\n" +
            "<ul class=\"chapter\">";
    static String dir_end_flag = "</ul>";

    static String chapterUrl_start_flag = "<li><a href=\"";
    static String chapterUrl_end_flag = "\">第";

    public static String getChapterList(String body) {
        return getContent(body, chapterUrl_start_flag, chapterUrl_end_flag);
    }

    public static String getDirListBody(String body) {
        return getContent(body, dir_start_flag, dir_end_flag);
    }

    public static List<String> getDirList(String body) {
        List<String> list = new ArrayList<>();
        int startIdx = 0;
        do {
            startIdx = body.indexOf(chapterUrl_start_flag, startIdx);
            if (startIdx < 0) {
                break;
            }

            startIdx += chapterUrl_start_flag.length();
            int endIdx = body.indexOf(chapterUrl_end_flag, startIdx);

            if (endIdx < 0) {
                break;
            }

            list.add(host + body.substring(startIdx, endIdx));
        } while (true);
        return list;
    }

    static String next_dirList_start_flag = "<span class=\"right\">\n" +
            "<a href=\"";
    static String next_dirList_end_flag = "\" class=\"onclick\">下一页</a>";

    public static String getNextDirList(String body) {
        return host + getContent(body, next_dirList_start_flag, next_dirList_end_flag);
    }
}
